package cn.wolfcode.shop.cloud.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class CookieUtil {
    public static final String COOKIE_NAME= "userToken";

    public static void addCoolie(String cookieName,String cookieVal, int expireTime, HttpServletResponse response){
        Cookie cookie = new Cookie(cookieName,cookieVal);
        cookie.setDomain("localhost");
        cookie.setPath("/");
        cookie.setMaxAge(expireTime);
        //响应给浏览器
        response.addCookie(cookie);
    }

    public static String getToken(HttpServletRequest request, String cookieName) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null || cookies.length == 0) {
            return null;
        }
        for (Cookie cookie : cookies) {
            if(cookie.getName().equals(cookieName)){
                return cookie.getValue();
            }
        }
        return null;
    }
}
